package com.spboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spboot.entity.Information;
import com.spboot.entity.Type;
import com.spboot.page.PageInfo;
import com.spboot.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/houtai")
public class HouTaiController {

    @Autowired
    InformationService informationService;

    @RequestMapping("hindex")
    public String index(){
       return "Hindex";
    }

    /**
     * 根据分类查询分类内容
     * @param typeid
     * @param model
     * @return
     */
    @RequestMapping("Hinformation")
    public String Hinformation(Long typeid,@RequestParam(defaultValue = "1", required = false) Long pageno, Model model){
        QueryWrapper<Information> queryWrapper = new QueryWrapper<>();
        PageInfo<Information> pageInfo = new PageInfo<>(pageno, 6);
        pageInfo = (PageInfo<Information>) informationService.selectdescribeb(typeid,pageInfo,queryWrapper);

        List<Information> inforlist = new ArrayList<>();//存放根据typeid查到的信息

        List<String> selectdescribeversion;
        List<String> selectdescribename;
        List<String> selectdescribeprice;

        Map<String,List<String>> versionmap=new HashMap<>();//存放版本
        Map<String,List<String>> namemap=new HashMap<>();//存放颜色
        Map<String,List<String>> pricemap=new HashMap<>();//存放价格


        inforlist =informationService.selectdescribea(typeid);
        Integer id;
        for (int i = 0;i < inforlist.size();i++){
            selectdescribeversion = new ArrayList<>();
            selectdescribename = new ArrayList<>();
            selectdescribeprice = new ArrayList<>();


            id=inforlist.get(i).getInformationid();

            selectdescribeversion=informationService.selectdescribeversion((long) id);
            selectdescribename=informationService.selectdescribename((long) id);
            selectdescribeprice=informationService.selectdescribeprice((long) id);


            versionmap.put(id.toString(),selectdescribeversion);
            namemap.put(id.toString(),selectdescribename);
            pricemap.put(id.toString(),selectdescribeprice);
        }
        model.addAttribute("typeid",typeid);
        model.addAttribute("inforlist",pageInfo);
        model.addAttribute("versionmap",versionmap);
        model.addAttribute("namemap",namemap);
        model.addAttribute("pricemap",pricemap);
        return "Htypeinformation";
    }


    /**
     * 后台根据informationid查询名字和详情
     * @param informationid
     * @param model
     * @return
     */
    @RequestMapping("hedit")
    public String hedit(Long informationid,Model model){
        List<String> selectdescribeversion = new ArrayList<>();
        List<String> selectdescribename = new ArrayList<>();
        List<String> selectdescribeprice = new ArrayList<>();

        selectdescribeversion=informationService.selectdescribeversion(informationid);
        selectdescribename=informationService.selectdescribename(informationid);
        selectdescribeprice=informationService.selectdescribeprice(informationid);

        model.addAttribute("versionmap",selectdescribeversion);
        model.addAttribute("namemap",selectdescribename);
        model.addAttribute("pricemap",selectdescribeprice);

        model.addAttribute("information",informationService.hselectinformation(informationid));
        return "Hedit";
    }

    @RequestMapping("/bbing")
    @ResponseBody
    public List<String> hbing(String versiontype, Long informationid, Model model){
        String version =  versiontype.replace(" ","+");
        List<String> bingpricelist = new ArrayList<>();
        bingpricelist=informationService.bingprice(versiontype, informationid);
        return bingpricelist;
    }

    /**
     * 后台修改商品信息
     * @param name
     * @param describea
     * @param informationid
     * @return
     */
    @RequestMapping("Hupdate")
    public String Hupdate(String name,String describea,Long informationid){
        int sum= informationService.Hupdate(name, describea, informationid);
        if(sum>0){
            return "redirect:/houtai/hedit?informationid="+informationid;
        }
        return "redirect:Hupdate";
    }
}
